package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.linlinjava.litemall.db.dao.LitemallTaxMapper;
import org.linlinjava.litemall.db.domain.LitemallTax;
import org.linlinjava.litemall.db.domain.LitemallTaxExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

 

/**
 * @author ：stephen
 * @date ：Created in 2020/5/13 16:31
 * @description：TODO
 */
@Service
public class LitemallTaxService extends MPJBaseServiceImpl<LitemallTaxMapper,LitemallTax> {
    @Resource
    private LitemallTaxMapper litemallTaxMapper;


    public List<LitemallTax> selectEnabledByIds(List<Integer> idLs) {
        return litemallTaxMapper.selectList(Wrappers.lambdaQuery(LitemallTax.class)
                .eq(LitemallTax::getEnable,1)
                .in(LitemallTax::getId,idLs));
    }




    public List<LitemallTax> getAll(Byte b){
        LitemallTaxExample example = new LitemallTaxExample();
        example.or().andTypeNotEqualTo(b);
        return litemallTaxMapper.selectByExample(example);
    }


    public List<LitemallTax> queryByType(Byte type) {
        LitemallTaxExample example = new LitemallTaxExample();
        example.or().andTypeEqualTo(type);
        return litemallTaxMapper.selectByExample(example);
    }
    
    public List<LitemallTax> queryByRegionIdAndCode(Integer regionId,String code) {
    	LitemallTaxExample example = new LitemallTaxExample();
    	example.or().andRegionIdEqualTo(regionId).andCodeEqualTo(code).andDeletedEqualTo(false).andEnableEqualTo(true);
    	return litemallTaxMapper.selectByExample(example);
    }


    public LitemallTax findById(Integer id) {
        return litemallTaxMapper.selectByPrimaryKey(id);
    }

    public IPage<LitemallTax> querySelective(Integer regionId, String code, Integer page, Integer size, String sort, String order) {

        LambdaQueryWrapper<LitemallTax> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(regionId != null,LitemallTax::getRegionId,regionId);
        lambdaQueryWrapper.eq(!StringUtils.isEmpty(code),LitemallTax::getCode,code);
        lambdaQueryWrapper.eq(LitemallTax::getDeleted,0);
        return litemallTaxMapper.selectPage(new Page(page,size),lambdaQueryWrapper);

       /* LitemallTaxExample example = new LitemallTaxExample();
        LitemallTaxExample.Criteria criteria = example.or();
        LitemallTaxExample.Criteria criteria1 = example.or();
        LitemallTaxExample.Criteria criteria2 = example.or();

        if (regionId != null) {
            criteria.andRegionIdEqualTo(regionId);
            criteria1.andFirstRegionIdEqualTo(regionId);
            criteria2.andSecondRegionIdEqualTo(regionId);
        }

        if(!StringUtils.isEmpty(code)){
            criteria.andCodeEqualTo(code);
            criteria1.andCodeEqualTo(code);
            criteria2.andCodeEqualTo(code);
        }

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        criteria.andDeletedEqualTo(false);
        criteria1.andDeletedEqualTo(false);
        criteria2.andDeletedEqualTo(false);
        //PageHelper.startPage(page, size);


        return litemallTaxMapper.selectByExample(new Page(page,size),example);*/
    }

    public int add(LitemallTax record){
        record.setAddTime(LocalDateTime.now());
        record.setUpdateTime(LocalDateTime.now());
        return litemallTaxMapper.insertSelective(record);
    }

    public int update(LitemallTax record){
        record.setUpdateTime(LocalDateTime.now());
        return litemallTaxMapper.updateByPrimaryKeySelective(record);
    }

    public List<LitemallTax> queryByRegionIds(List<Integer> regionIds) {
        if(regionIds == null || regionIds.size() == 0){
            return new ArrayList<>();
        }
        LitemallTaxExample example = new LitemallTaxExample();
        example.or().andRegionIdIn(regionIds).andDeletedEqualTo(false);
        return litemallTaxMapper.selectByExample(example);
    }

    public boolean exist(LitemallTax tax) {
        LitemallTaxExample example = new LitemallTaxExample();
        example.or().andRegionIdEqualTo(tax.getRegionId()).andCodeEqualTo(tax.getCode()).andDeletedEqualTo(false);
        return litemallTaxMapper.countByExample(example) > 0;
    }

    public int deleteById(Integer id) {
        return litemallTaxMapper.logicalDeleteByPrimaryKey(id);
    }
    
    public List<LitemallTax> queryEnabledByRegionIds(List<Integer> regionIdLs) {
        LitemallTaxExample example = new LitemallTaxExample();
        LitemallTaxExample.Criteria criteria = example.or();

        criteria.andRegionIdIn(regionIdLs).andEnableEqualTo(true).andDeletedEqualTo(false);
        return litemallTaxMapper.selectByExample(example);
    }
    
    public List<LitemallTax> queryEnabledByIds(List<Integer> idLs) {
    	LitemallTaxExample example = new LitemallTaxExample();
    	LitemallTaxExample.Criteria criteria = example.or();
    	
    	criteria.andIdIn(idLs).andEnableEqualTo(true).andDeletedEqualTo(false);
    	return litemallTaxMapper.selectByExample(example);
    }
}
